﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LamSon.Commons;

namespace LamSon.Controllers
{
    public class BaseController : Controller
    {
        //
        // GET: /Base/
        public static int totalRow = 0;
        private static int limitPage = 50;
        public static int LimitPage
        {
            get { return limitPage; }
            set { limitPage = value; }
        }
        private static int pagecurr = 1;

        public static int Pagecurr
        {
            get { return BaseController.pagecurr; }
            set { BaseController.pagecurr = value; }
        }
        
        public ActionResult BaseIndex(int id,string name)
        {
            
            if (id == 1)
            {
                Pagecurr = 1;
                LimitPage = 50;
                SetActiveMenu("class=\"MActive\"", null, null, null, null);
            }
            if (id == 2)
            {
                Pagecurr = 1;
                LimitPage = 50;
                SetActiveMenu("class=\"MActive\"", null, null, null, null);
            }
            if (id == 3)
            {
                Pagecurr = 1;
                LimitPage = 50;
                ReturnViewData_NKBH();
                getTotalMoney();
                
                SetViewDataInfoSearch_NKBH(null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
                SetActiveMenu(null, "class=\"MActive\"", null, null, null);
            }
            if (id == 4)
            {
                Pagecurr = 1;
                LimitPage = 50;
                SetActiveMenu(null, "class=\"MActive\"", null, null, null);
            }
            if (id == 5)
            {
                Pagecurr = 1;
                LimitPage = 50;
                ReturnViewData_ChuHang();
                SetViewDataInfoSearch_ChuHang(null, null, null);
                SetActiveMenu(null, null, "class=\"MActive\"", null, null);
            }
            if (id == 6)
            {
                Pagecurr = 1;
                LimitPage = 50;
                ReturnViewData_KhachHang();
                SetViewDataInfoSearch_KhachHang(null,null,null);
                SetActiveMenu(null, null, "class=\"MActive\"", null, null);
            }
            if (id == 7)
            {
                Pagecurr = 1;
                LimitPage = 50;
                SetActiveMenu(null, null, null, "class=\"MActive\"", null);
            }
            if (id == 8)
            {
                Pagecurr = 1;
                LimitPage = 50;
                SetActiveMenu(null, null, null, "class=\"MActive\"", null);
            }
            if (id == 9)
            {
                Pagecurr = 1;
                LimitPage = 50;
                ReturnViewData_RuongMuoiNha();
                SetViewDataInfoSearch_RuongMuoiNha(null,null,null, null, null);
                SetActiveMenu(null, null, null, null, "class=\"MActive\"");
            }
            if (id == 10)
            {
                Pagecurr = 1;
                LimitPage = 50;
                SetActiveMenu(null, null, null, null, "class=\"MActive\"");
            }
            if (id == 11)
            {
                Pagecurr = 1;
                LimitPage = 50;
                SetActiveMenu(null, null, null, null, "class=\"MActive\"");
            }
            SetActiveMenu("class=\"MActive\"", "class=\"MActive\"", "class=\"MActive\"", "class=\"MActive\"", "class=\"MActive\"");
            return View(name);
        }
        public void SetViewDataInfoSearch_NKBH(string ngayban_sendedit, string chuhang_sendedit, string diachi_sendedit, string phanloai_sendedit,
            string thanhtoan_sendedit, string xetangbo_sendedit, string diadiemtangbo_sendedit, string xevanchuyen_sendedit, string sanpham_sendedit,
            string nhapxuat_sendedit, string bentravanchuyen_sendedit, string khachhang_sendedit, string ghichu_sendedit,string tracuoctb_sendedit,string tracuocvc_sendedit, string pagesize_sendedit, string page_sendedit)
        {
            ViewData["ngayban_sendedit"] = ngayban_sendedit;
            ViewData["chuhang_sendedit"] = chuhang_sendedit;
            ViewData["diachi_sendedit"] = diachi_sendedit;
            ViewData["phanloai_sendedit"] = phanloai_sendedit;
            ViewData["thanhtoan_sendedit"] = thanhtoan_sendedit;
            ViewData["xetangbo_sendedit"] = xetangbo_sendedit;
            ViewData["diadiemtangbo_sendedit"] = diadiemtangbo_sendedit;
            ViewData["xevanchuyen_sendedit"] = xevanchuyen_sendedit;
            ViewData["sanpham_sendedit"] = sanpham_sendedit;
            ViewData["nhapxuat_sendedit"] = nhapxuat_sendedit;
            ViewData["bentravanchuyen_sendedit"] = bentravanchuyen_sendedit;
            ViewData["khachhang_sendedit"] = khachhang_sendedit;
            ViewData["ghichu_sendedit"] = ghichu_sendedit;
            ViewData["tracuoctb_sendedit"] = tracuoctb_sendedit;
            ViewData["tracuocvc_sendedit"] = tracuocvc_sendedit;
            ViewData["pagesize_sendedit"] = pagesize_sendedit;
            ViewData["page_sendedit"] = page_sendedit;
        }
        public void SetViewDataInfoSearch_KhachHang(string khachhang_sendedit, string pagesize_sendedit, string page_sendedit)
        {
            ViewData["khachhang_sendedit"] = khachhang_sendedit;
            ViewData["pagesize_sendedit"] = pagesize_sendedit;
            ViewData["page_sendedit"] = page_sendedit;
        }
        public void SetViewDataInfoSearch_ChuHang(string khachhang_sendedit, string pagesize_sendedit, string page_sendedit)
        {
            ViewData["khachhang_sendedit"] = khachhang_sendedit;
            ViewData["pagesize_sendedit"] = pagesize_sendedit;
            ViewData["page_sendedit"] = page_sendedit;
        }
        public void SetViewDataInfoSearch_RuongMuoiNha(string ruongmuoi_sendedit, string nguoilam_sendedit, string noinhap_sendedit, string pagesize_sendedit, string page_sendedit)
        {
            ViewData["ruongmuoi_sendedit"] = ruongmuoi_sendedit;
            ViewData["nguoilam_sendedit"] = nguoilam_sendedit;
            ViewData["noinhap_sendedit"] = noinhap_sendedit;
            ViewData["pagesize_sendedit"] = pagesize_sendedit;
            ViewData["page_sendedit"] = page_sendedit;
        }
        public void ReturnViewData_NKBH()
        {
            ViewData["IdForm"] = 3;
            ViewData["ListNKBH"] = NhatKyBanHangController.getListNKBH(null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
            ViewData["PageCurr"] = BaseController.Pagecurr;
            ViewData["LimitPage"] = BaseController.LimitPage;
            if (totalRow % LimitPage == 0)
            {
                ViewData["TotalPage"] = totalRow / LimitPage;
            }
            else
            {
                ViewData["TotalPage"] = totalRow / LimitPage + 1;
            }
            ViewData["DataNKBH"] = null;
            ViewData["NgayBan"] = NhatKyBanHangController.bindComboxSearch("NgayBan");
            ViewData["ChuHang"] = NhatKyBanHangController.bindComboxSearch("ChuHang");
            ViewData["DiaChi"] = NhatKyBanHangController.bindComboxSearch("DiaChi");
            ViewData["PL"] = NhatKyBanHangController.bindComboxSearch("PL");
            ViewData["TraTienMuoiNhap"] = NhatKyBanHangController.bindComboxSearch("TraTienMuoiNhap");
            ViewData["SoXeTangBo"] = NhatKyBanHangController.bindComboxSearch("SoXeTangBo");
            ViewData["DiaDiemTangBo"] = NhatKyBanHangController.bindComboxSearch("DiaDiemTangBo");
            ViewData["SoXeVanChuyen"] = NhatKyBanHangController.bindComboxSearch("SoXeVanChuyen");
            ViewData["SanPham"] = NhatKyBanHangController.bindComboxSearch("SanPham");
            ViewData["NhapXuat"] = NhatKyBanHangController.bindComboxSearch("NhapXuat");
            ViewData["BenTraVanChuyen"] = NhatKyBanHangController.bindComboxSearch("BenTraVanChuyen");
            ViewData["KhachHang"] = NhatKyBanHangController.getListCustomer(5);
            ViewData["NoThu"] = NhatKyBanHangController.bindComboxSearch("NoThu");
            ViewData["GhiChu"] = NhatKyBanHangController.bindComboxSearch("GhiChu");
            ViewData["TraCuocTB"] = NhatKyBanHangController.bindComboxSearch("TraCuocTB");
            ViewData["TraCuocVC"] = NhatKyBanHangController.bindComboxSearch("TraCuocVC");
        }
        public void ReturnViewData_KhachHang()
        {
            ViewData["IdForm"] = 6;
            ViewData["ListKhachHang"] = KhachHangController.getListKhachHang(null);
            ViewData["PageCurr"] = BaseController.Pagecurr;
            ViewData["LimitPage"] = BaseController.LimitPage;
            if (totalRow % LimitPage == 0)
            {
                ViewData["TotalPage"] = totalRow / LimitPage;
            }
            else
            {
                ViewData["TotalPage"] = totalRow / LimitPage + 1;
            }
            ViewData["DataKhachHang"] = null;
            ViewData["KhachHang"] = NhatKyBanHangController.getListCustomer(5);
        }
        public void ReturnViewData_ChuHang()
        {
            ViewData["IdForm"] = 5;
            ViewData["ListChuHang"] = ChuHangController.getListChuHang(null);
            ViewData["PageCurr"] = BaseController.Pagecurr;
            ViewData["LimitPage"] = BaseController.LimitPage;
            if (totalRow % LimitPage == 0)
            {
                ViewData["TotalPage"] = totalRow / LimitPage;
            }
            else
            {
                ViewData["TotalPage"] = totalRow / LimitPage + 1;
            }
            ViewData["DataChuHang"] = null;
            ViewData["ChuHang"] = NhatKyBanHangController.getListCustomer(6);
        }
        public void ReturnViewData_RuongMuoiNha()
        {
            ViewData["IdForm"] = 9;
            ViewData["ListRuongMuoiNha"] = RuongMuoiNhaController.getListRuongMuoiNha(null,null,null);
            ViewData["PageCurr"] = BaseController.Pagecurr;
            ViewData["LimitPage"] = BaseController.LimitPage;
            if (totalRow % LimitPage == 0)
            {
                ViewData["TotalPage"] = totalRow / LimitPage;
            }
            else
            {
                ViewData["TotalPage"] = totalRow / LimitPage + 1;
            }
            ViewData["DataRuongMuoiNha"] = null;
            ViewData["NgayThang"] = RuongMuoiNhaController.bindComboxSearch_RuongMuoiNha("NgayThang");
            ViewData["RuongMuoi"] = RuongMuoiNhaController.bindComboxSearch_RuongMuoiNha("RuongMuoi");
            ViewData["NguoiLam"] = RuongMuoiNhaController.bindComboxSearch_RuongMuoiNha("NguoiLam");
            ViewData["NoiNhap"] = RuongMuoiNhaController.bindComboxSearch_RuongMuoiNha("NoiNhap");
        }
        public void getTotalMoney()
        {
            double totalCuocXeTangBo = 0;
            double totalTienMuoiNhap = 0;
            double totalTTCuocTB = 0;
            double totalSoLuong = 0;
            double totalTTCuocVC = 0;
            double totalBBBV = 0;
            double totalTTNhap = 0;
            double totalTTXuat = 0;
            double totalTienMuoiXuat = 0;
            double totalDoanhThu = 0;
            double totalNoThu = 0;
            List<Data.DBOES.NhatKyBanHang> objNhatKyBanHangListAll = new List<Data.DBOES.NhatKyBanHang>();
            objNhatKyBanHangListAll = Data.Controllers.NhatKyBanHangController.FindItems(new Data.DBOES.NhatKyBanHang());
            foreach (Data.DBOES.NhatKyBanHang nkbh in objNhatKyBanHangListAll)
            {
                totalCuocXeTangBo += DataTypeProtect.ProtectDouble(nkbh.CuocXeTangBo, 0);
                totalTienMuoiNhap += (DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0) * DataTypeProtect.ProtectDouble(nkbh.GiaNhap, 0));
                totalTTCuocTB += (DataTypeProtect.ProtectDouble(nkbh.CuocXeTangBo, 0) * DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0));
                totalSoLuong += DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0);
                totalTTCuocVC += DataTypeProtect.ProtectDouble(nkbh.CuocXeVanChuyen, 0) * DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0);
                totalBBBV += DataTypeProtect.ProtectDouble(nkbh.GiaBaoBiBocVac, 0);
                totalTTNhap += ((DataTypeProtect.ProtectDouble(nkbh.CuocXeTangBo, 0) + DataTypeProtect.ProtectDouble(nkbh.GiaNhap, 0) + DataTypeProtect.ProtectDouble(nkbh.GiaBaoBiBocVac, 0)) * DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0));
                totalTTXuat += (DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0) * DataTypeProtect.ProtectDouble(nkbh.GiaXuat, 0));
                totalTienMuoiXuat += ((DataTypeProtect.ProtectDouble(nkbh.GiaXuat, 0) - DataTypeProtect.ProtectDouble(nkbh.CuocXeVanChuyen, 0)) * DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0));
                totalDoanhThu += ((DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0) * DataTypeProtect.ProtectDouble(nkbh.GiaXuat, 0)) - ((DataTypeProtect.ProtectDouble(nkbh.CuocXeTangBo, 0) + DataTypeProtect.ProtectDouble(nkbh.GiaNhap, 0) + DataTypeProtect.ProtectDouble(nkbh.GiaBaoBiBocVac, 0)) * DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0)) - (DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0) * DataTypeProtect.ProtectDouble(nkbh.CuocXeVanChuyen, 0)));
                totalNoThu += DataTypeProtect.ProtectDouble(nkbh.NoThu, 0);
            }
            ViewData["LabCuocXeTangBo"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalCuocXeTangBo));
            ViewData["LabSoLuong"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalSoLuong));
            ViewData["LabTTCuocVC"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalTTCuocVC));
            ViewData["LabTTNhap"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalTTNhap));
            ViewData["LabTTXuat"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalTTXuat));
            ViewData["LabTienMuoiXuat"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalTienMuoiXuat));
            ViewData["LabTienMuoiNhap"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalTienMuoiNhap));
            ViewData["LabDoanhThu"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalDoanhThu));
            ViewData["LabTTCuocTB"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalTTCuocTB));
            ViewData["LabNoThu"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalNoThu));
        }
        private void SetActiveMenu(string ac1, string ac2, string ac3, string ac4, string ac5)
        {
            ViewData["HeThong"] = ac1;
            ViewData["NhapXuat"] = ac2;
            ViewData["ChuKhach"] = ac3;
            ViewData["TaiChinh"] = ac4;            
            ViewData["Khac"] = ac5;
        }
    }
}
